package com.ruoyi.garbage.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.garbage.domain.ChallengeDetail;
import com.ruoyi.garbage.domain.ChallengeResult;
import com.ruoyi.garbage.mapper.ChallengeDetailMapper;
import com.ruoyi.garbage.mapper.ChallengeResultMapper;
import com.ruoyi.garbage.service.IChallengeResultService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 挑战结果+详情记录Service业务层处理
 *
 * @author 刘哲贤
 * @date 2023-01-07
 */
@Service
public class ChallengeResultServiceImpl implements IChallengeResultService {
    @Resource
    private ChallengeResultMapper challengeResultMapper;
    @Resource
    private ChallengeDetailMapper challengeDetailMapper;

    /**
     * 查询挑战结果+详情记录
     *
     * @param id 挑战结果+详情记录主键
     * @return 挑战结果+详情记录
     */
    @Override
    public ChallengeResult selectChallengeResultById(Long id) {
        return challengeResultMapper.selectChallengeResultById(id);
    }

    /**
     * 查询挑战结果+详情记录列表
     *
     * @param challengeResult 挑战结果+详情记录
     * @return 挑战结果+详情记录
     */
    @Override
    public List<ChallengeResult> selectChallengeResultList(ChallengeResult challengeResult) {
        return challengeResultMapper.selectChallengeResultList(challengeResult);
    }

    /**
     * 新增挑战结果+详情记录
     *
     * @param challengeResult 挑战结果+详情记录
     * @return 结果
     */
    @Override
    public int insertChallengeResult(ChallengeResult challengeResult) {
        return challengeResultMapper.insertChallengeResult(challengeResult);
    }

    /**
     * 修改挑战结果+详情记录
     *
     * @param challengeResult 挑战结果+详情记录
     * @return 结果
     */
    @Override
    public int updateChallengeResult(ChallengeResult challengeResult) {
        return challengeResultMapper.updateChallengeResult(challengeResult);
    }

    /**
     * 批量删除挑战结果+详情记录
     *
     * @param ids 需要删除的挑战结果+详情记录主键
     * @return 结果
     */
    @Override
    public int deleteChallengeResultByIds(Long[] ids) {
        return challengeResultMapper.deleteChallengeResultByIds(ids);
    }

    /**
     * 删除挑战结果+详情记录信息
     *
     * @param id 挑战结果+详情记录主键
     * @return 结果
     */
    @Override
    public int deleteChallengeResultById(Long id) {
        return challengeResultMapper.deleteChallengeResultById(id);
    }

    @Override
    public AjaxResult challengeResults(Map<String, Object> map) {
        Integer score = (Integer) map.get("score");
        String userName = (String) map.get("userName");
        List<LinkedHashMap> list = (List<LinkedHashMap>) map.get("list");
        List<ChallengeDetail> detailArrayList = new ArrayList<>();
        for (LinkedHashMap item : list) {
            ChallengeDetail detail = new ChallengeDetail();
            detail.setQuestionId((Integer) item.get("questionId"));
            detail.setGarbageName(item.get("garbageName").toString());
            detail.setGarbageType((Integer) item.get("garbageType"));
            detail.setSelectedType((Integer) item.get("selectedType"));
            detail.setWhether(detail.getGarbageType().equals(detail.getSelectedType()) ? 1 : 0);
            detailArrayList.add(detail);
        }
        ChallengeResult entity = new ChallengeResult();
        entity.setScore(score);
        entity.setResult(JSONObject.toJSONString(detailArrayList));
        entity.setUserName(userName);
        challengeResultMapper.insertChallengeResult(entity);
        challengeDetailMapper.insertBatch(detailArrayList);
        return AjaxResult.success();
    }
}
